package com.lam.common.components.easyexcel.export;

import java.io.OutputStream;
import java.util.Collections;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.lam.common.utils.CollectionUtil;

public class DefaultDataWriter<T> implements DataWriter<T> {

	@Override
	public List<WriteHandler> getWriteHandler() {
		return Collections.emptyList();
	}

	@Override
	public ExcelWriter createExcel(String filePath, Class<T> clazz) {

		return EasyExcel.write(filePath, clazz).build();
	}
	
	@Override
	public ExcelWriter createExcel(OutputStream out, Class<T> clazz) {
		
		return EasyExcel.write(out, clazz).build();
	}

	@Override
	public WriteSheet createSheet(String sheetName, int fileNo) {
		ExcelWriterSheetBuilder sheetBuilder = EasyExcel.writerSheet(sheetName);
		List<WriteHandler> handlers = getWriteHandler();
		if (CollectionUtil.isNotEmpty(handlers)) {
			for (WriteHandler writeHandler : handlers) {
				sheetBuilder.registerWriteHandler(writeHandler);
			}
		}
		return sheetBuilder.build();
	}

	@Override
	public void writeData(ExcelWriter excelWriter, WriteSheet writeSheet, List<T> data) {
		excelWriter.write(data, writeSheet);
	}

}
